package com.tjmc.finance.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjmc.finance.service.MemberInfoService;
import com.tjmc.recruit.admin.api.enums.PlatUserTypeEnum;
import com.tjmc.recruit.admin.api.entity.PlatformUserInfoEntity;
import com.tjmc.recruit.admin.api.feign.RemoteUserService;
import com.tjmc.recruit.common.core.constant.SecurityConstants;
import com.tjmc.recruit.common.core.exception.ErrorCodes;
import com.tjmc.recruit.common.core.util.MsgUtils;
import com.tjmc.recruit.common.core.util.R;
import com.tjmc.recruit.common.log.annotation.SysLog;
import com.tjmc.recruit.common.security.annotation.Inner;
import com.tjmc.recruit.finance.api.entity.MemberInfoEntity;
import com.tjmc.recruit.finance.api.enums.MemberStatusEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

/**
 * 会员表
 *
 * @author recruit
 * @date 2024-11-25 14:16:59
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/memberInfo")
@Tag(description = "memberInfo", name = "会员表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class MemberInfoController {

    private final MemberInfoService memberInfoService;
    private final RemoteUserService remoteUserService;
    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param memberInfo 会员表
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R getMemberInfoPage(@ParameterObject Page page, @ParameterObject MemberInfoEntity memberInfo) {
        LambdaQueryWrapper<MemberInfoEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(memberInfoService.page(page, wrapper));
    }


    /**
     * 通过条件查询会员表
     *
     * @param memberInfo 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询", description = "通过条件查询对象")
    @GetMapping("/details")
    public R getDetails(@ParameterObject MemberInfoEntity memberInfo) {
        return R.ok(memberInfoService.list(Wrappers.query(memberInfo)));
    }

    /**
     * 根据ID查询会员信息
     *
     * @param id ID
     * @return
     */
    @Operation(summary = "根据ID查询会员信息", description = "根据ID查询会员信息")
    @SysLog("根据ID查询会员信息")
    @GetMapping("/{id}")
    public R getMemberInfoById(@PathVariable("id") Long id) {
        return R.ok(memberInfoService.getMemberById(id));
    }

    /**
     * 根据用户ID查询会员信息
     *
     * @param accountId 用户ID
     * @return
     */
    @Operation(summary = "根据用户ID查询会员信息", description = "根据用户ID查询会员信息")
    @SysLog("根据用户ID查询会员信息")
    @GetMapping("/accountId")
    @Inner
    public R getMemberInfoByAccountID(@RequestParam("accountId") Long accountId) {
        return R.ok(memberInfoService.getMemberByUserId(accountId).getData());
    }

    /**
     * 新增会员表
     *
     * @param memberInfo 会员表
     * @return R
     */
    @Operation(summary = "新增会员表", description = "新增会员表")
    @SysLog("新增会员表")
    @PostMapping
    public R save(@RequestBody MemberInfoEntity memberInfo) {
        if (StrUtil.isBlank(memberInfo.getAccountId())) {
            return R.failed("用户ID不能为空");
        }
        R<PlatformUserInfoEntity> platformedUser = remoteUserService.platformUser(memberInfo.getAccountName(), SecurityConstants.FROM_IN);
        PlatformUserInfoEntity userInfo = platformedUser.getData();
        if (userInfo == null) {
            return R.failed(MsgUtils.getMessage(ErrorCodes.SYS_USER_QUERY_ERROR));
        }
        //子账号不允许新增会员
        if (userInfo.getUserType().equals(PlatUserTypeEnum.COMPANY)) {
            if (userInfo.getAccountType().equals(PlatUserTypeEnum.SUB)) {
                return R.failed(MsgUtils.getMessage(ErrorCodes.SYS_USER_QUERY_ERROR));
            }
        }
        MemberInfoEntity info = memberInfoService.getOne(new LambdaQueryWrapper<MemberInfoEntity>().eq(MemberInfoEntity::getAccountId, memberInfo.getAccountId()));
        if (info != null) {
            return R.failed("已存在会员信息");
        }
        memberInfo.setStatus(MemberStatusEnum.INACTIVE);
        return R.ok(memberInfoService.save(memberInfo));
    }

    /**
     * 修改会员表
     *
     * @param memberInfo 会员表
     * @return R
     */
    @Operation(summary = "修改会员表", description = "修改会员表")
    @SysLog("修改会员表")
    @PutMapping
    public R updateById(@RequestBody MemberInfoEntity memberInfo) {
        return R.ok(memberInfoService.updateById(memberInfo));
    }

    /**
     * 通过id删除会员表
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除会员表", description = "通过id删除会员表")
    @SysLog("通过id删除会员表")
    @DeleteMapping
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(memberInfoService.removeBatchByIds(CollUtil.toList(ids)));
    }

}
